package com.ygxsk.carhome.views.picker;


import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.ygxsk.carhome.R;
import com.ygxsk.integrated.retrofit.Parameterkey;

/**
 * 时间选择器，弹出框  包含年月日的
 * Created by ycuwq on 2018/1/6.
 *
 * @deprecated If you need to use it, please use support or androidx to achieve.
 */
@Deprecated
public class YearMonthDayDialogFragment extends DialogFragment {

    protected YearMonthDayPicker mYearMonthDayPicker;
    private int mSelectedYear = -1, mSelectedMonth = -1, mSelectedDay = -1;
    private OnYearMonthDayChooseListener mOnYearMonthDayChooseListener;
    private boolean mIsShowAnimation = true;
    protected TextView mCancelButton, mDecideButton;

    public void setOnYearMonthDayChooseListener(OnYearMonthDayChooseListener _onYearMonthDayChooseListener) {
        mOnYearMonthDayChooseListener = _onYearMonthDayChooseListener;
    }

    public void showAnimation(boolean show) {
        mIsShowAnimation = show;
    }

    /**
     * 2022-11-14 14:37:33 新增的功能
     */
    public static YearMonthDayDialogFragment newInstance(int number) {

        Bundle args = new Bundle();

        YearMonthDayDialogFragment fragment = new YearMonthDayDialogFragment();
//        args.putInt("num", number);
        args.putInt(Parameterkey.year_month_day_num, number);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //View view = inflater.inflate(R.layout.dialog_date, container);

        View view = inflater.inflate(R.layout.dialogyearmonthday, container);
        mYearMonthDayPicker = (YearMonthDayPicker) view.findViewById(R.id.yearmonthdaypicker_dialog);

        mCancelButton = (TextView) view.findViewById(R.id.btn_dialog_yearmonthday_cancel);
        mDecideButton = (TextView) view.findViewById(R.id.btn_dialog_yearmonthday_decide);

        TextView mClearButton = (TextView) view.findViewById(R.id.btn_dialog_yearmonth_clear);

        if (mClearButton != null) {
            mClearButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //清空重置的按钮；
                    if (mOnYearMonthDayChooseListener != null) {
                        //这个特殊-设置为-1;什么都没有选中；
                        mOnYearMonthDayChooseListener.onYearMonthDayChoose(-1, -1, -1);
                    }

                    dismiss();

                }
            });
        }

        if (mCancelButton != null) {
            mCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        if (mDecideButton != null) {
            mDecideButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnYearMonthDayChooseListener != null) {
                        mOnYearMonthDayChooseListener.onYearMonthDayChoose(mYearMonthDayPicker.getYear(),
                                mYearMonthDayPicker.getMonth(), mYearMonthDayPicker.getDay());
                    }
                    dismiss();
                }
            });
        }

        if (mSelectedYear > 0) {
            setSelectedDate();
        }

        initChild();
        return view;
    }

    protected void initChild() {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.DatePickerBottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setContentView(R.layout.dialogyearmonthday);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        Window window = dialog.getWindow();
        if (window != null) {
            if (mIsShowAnimation) {
                window.getAttributes().windowAnimations = R.style.DatePickerDialogAnim;
            }
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM; // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
            lp.dimAmount = 0.35f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        return dialog;
    }

    public void setSelectedDate(int year, int month, int day) {
        mSelectedYear = year;
        mSelectedMonth = month;
        mSelectedDay = day;
        setSelectedDate();
    }

    private void setSelectedDate() {

        if (mYearMonthDayPicker != null) {
            mYearMonthDayPicker.setDate(mSelectedYear, mSelectedMonth, mSelectedDay, false);
        }

    }

    public interface OnYearMonthDayChooseListener {
        void onYearMonthDayChoose(int year, int month, int day);
    }

}
